//
//  ZJIssueOrganizationSelectView.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/7/27.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJIssueOrganizationSelectView.h"

@interface ZJIssueOrganizationSelectView ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>

/** tableview */
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation ZJIssueOrganizationSelectView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor zj_colorWithHexString:@"000000" alpha:0.5];
        [self addSubview:self.tableView];
        UITapGestureRecognizer *tapGest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(action_tapgester)];
        [self addGestureRecognizer:tapGest];
        
        ///首先让UITapGestureRecognizer不要吃掉子窗口的事件：
        tapGest.cancelsTouchesInView = NO;
        ///然后设置自身的代理：
        tapGest.delegate = self;
    }
    return self;
}




- (void)updateTableView{
    NSInteger maxNumber = 10;
    if (self.dataArray.count <= maxNumber) {
        self.tableView.height = self.dataArray.count * 40 * [UIScreen zj_screenWScale];
    }else{
        self.tableView.height = maxNumber * 40 * [UIScreen zj_screenWScale];
    }
    self.tableView.center = self.center;
    [self.tableView reloadData];
}

#pragma mark - 点击事件
- (void)action_tapgester{
    [self dissView];
}

//消失
- (void)dissView{
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.5 animations:^{
        weakSelf.alpha = 0;
    }completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
    }];
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    // 若为UITableViewCellContentView（即点击了tableViewCell），则不截获Touch事件
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    }
    return  YES;
}

#pragma mark - UITableView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    ZJIssueOrganModel *model = self.dataArray[indexPath.row];
    cell.textLabel.text = model.DEPT_NAME;
    cell.textLabel.textColor = [UIColor textBlackColor];
    cell.textLabel.font = [UIFont titleTextFont];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40 * [UIScreen zj_screenWScale];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.delegate) {
        [self.delegate ZJIssueOrganizationSelectViewDelegate_cellSelected_Model:self.dataArray[indexPath.row]];
    }
    [self dissView];
}




#pragma mark - Setter
- (void)setDataArray:(NSMutableArray *)dataArray{
    _dataArray = dataArray;
    [self updateTableView];
}

#pragma mark - Getters
- (UITableView *)tableView{
    if (_tableView == nil){
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.width - 50 * [UIScreen zj_screenWScale], 100) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.center = self.center;
        _tableView.layer.cornerRadius = 4 * [UIScreen zj_screenWScale];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    }
    return _tableView;
}

@end
